package com.wontube.client.extension.utils;

import java.util.Random;

/**
 * @author tanggf(2012-5-7)
 *
 */
public class RandomUtils {
	private final static char[] WORDS = new char[]{
		'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
		'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
		'0','1','2','3','4','5','6','7','8','9'		
	};
	private final static int WORDS_LENGTH = WORDS.length;
	
	/**
	 * [0 - end)
	 * @param end
	 * @return
	 */
	public static int randomInt(int end){
		return randomInt(0, end);
	}
	
	/**
	 * [start - end)
	 * @param start
	 * @param end
	 * @return
	 */
	public static int randomInt(int start, int end){
		if(end <= start){
			return 0;
		}
		int len = end - start;
		if(len <= 0){
			return 0;
		}
		
		Random r = new Random();		
		return (r.nextInt(len) + start);		
	}
	
	/**
	 * random string
	 * @param num
	 * @return
	 */
	public static String randomText(int num){
		if(num <= 0){
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < num; i++) {
			sb.append(WORDS[randomInt(WORDS_LENGTH)]);
		}
		return sb.toString();
	}	
}
